package com.cmj.service.impl;

import com.cmj.dao.MenuMapper;
import com.cmj.dao.UserMapper;
import com.cmj.domain.SystemConstants;
import com.cmj.domain.entity.LoginUser;
import com.cmj.domain.entity.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * 这是注释
 */
@Service
public class UserDetailsServiceImpl implements UserDetailsService {
    @Autowired
    private UserMapper userMapper;

    @Resource
    private MenuMapper menuMapper;

    @Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
        //根据用户名查询用户信息
        User user = userMapper.getUserByName(username);
        //判断是否查到用户
        if (user == null){
            throw new RuntimeException("用户不存在");
        }
        //查看是否为管理员，是就返回拥有权限
        if(user.getType().equals(SystemConstants.ADMIN)){
            List<String> list = menuMapper.selectPermsByUserId(user.getId());
            return new LoginUser(user,list);
        }

        //LoginUser实现UserDetails，重写其中的各种
        return new LoginUser(user,null);
    }
}
